module service.IdGeneratorService;

import hunt.logging;
import service.SystemConfigService;
import std.conv : to;



class IdGeneratorService {

  public
  {
      static IdGeneratorService instance()
      {
          if(inst is null)
          {
              inst = new IdGeneratorService;
          }
          return inst;
      }

     string getNextId() {
      synchronized (this){
        if (lastId < nextId) {
          getNewBlock();
        }
        long _nextId = nextId++;
        return to!string(_nextId);
      }
    }
  }

  private void getNewBlock()
  {
    IdBlock idBlock = SystemConfigService.getNextId(idBlockSize);
    if (idBlock is null)
    {
      logError("IdBlock is null .....");
    }
    this.nextId = idBlock.getNextId();
    this.lastId = idBlock.getLastId();
  }

  private {

    this() {
    }

    int idBlockSize = 10;
    long nextId;
    long lastId = -1;
    static IdGeneratorService inst = null;
  }
}

